//
//  Public+Ex.m
//  PerfumeTesting
//
//  Created by Michael Shen on 15/5/6.
//  Copyright (c) 2015年 Dextrys. All rights reserved.
//

#import "Public+Ex.h"

@implementation Public_Ex

@end
@interface BlockAlertView()
@property (nonatomic,strong) ClickBolck block;
@end

@implementation BlockAlertView
@synthesize block = _block;

+ (UIAlertView *)showAlertWithTitle:(NSString *)title message:(NSString *)message alertViewStyle:(UIAlertViewStyle)alertViewStyle block:(ClickBolck)block cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION {
    BlockAlertView *alertView = [[BlockAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles,nil];
    alertView.delegate = alertView;
    alertView.alertViewStyle = alertViewStyle;
    alertView.block = block;
    if (otherButtonTitles){
        va_list args;
        va_start(args, otherButtonTitles);
        for (NSString *buttonTitle = va_arg(args,NSString*); buttonTitle != nil; buttonTitle = va_arg(args,NSString*)) {
            [alertView addButtonWithTitle:buttonTitle];
        }
        va_end(args);
    }
    
    [alertView show];
    
    return alertView;
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView {
    if (alertView.alertViewStyle == UIAlertViewStylePlainTextInput) {
        UITextField *textField = [alertView textFieldAtIndex:0];
        if (textField.text.length == 0) {
            return NO;
        }
    }
    return YES;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (self.block != nil) {
        self.block(self,buttonIndex);
    }
}

@end